<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Description of member_model
 * 
 * @author Subha Sundar Das (subhasundar.das@gmail.com)
 */
class Member_model extends CI_Model {

    function __construct() {
        parent::__construct();
        log_message('debug', 'Model Initialized');
    }

//--Delete Member (Subha)--//
    function delete_account($id) {
        $del_listing = "Delete * from listing_master where user=$id";
        $del_review = "Delete * from review_master where mem_id=$id";
        $del_member = "Delete * from member_master where id=$id";
        $del_terms = "Delete * from terms where listing_id=$id";

        //$query = $this->db->query($del_listing);
        //$query = $this->db->query($del_review);
        //$query = $this->db->query($del_member);
    }

//-- Get Member Listings (Subha)--//
    function get_member_listings($id) {
        if ($id) {
            $sql = "select * from listing_master where user = $id order by id desc";
            $query = $this->db->query($sql);
            return $query->result();
        }
    }

//--Get Memebr Information (Subha)--//
    function get_member($id) {
        if ($id) {
            $sql = "select * from member_master where id = $id and status=51 limit 1";
            $query = $this->db->query($sql);
            return $query->row();
        }
    }

    //--Get Memebrs Information (Joy)--//
    function get_members($offset=NULL, $no=NULL, $status=NULL) {
        if (!$status) {
            $sql = "select * from member_master limit $offset, $no";
        } else {
            $sql = "select * from member_master where status='$status' limit $offset, $no";
        }

        $query = $this->db->query($sql);
        return $query->result();
    }

//-- INSERT NEW MWMBER ---//    
    function append_member($post) {
        if ($post) {
            $this->db->insert('member_master', $post);
            return "<strong>Thank you!</strong> &nbsp; 
                Successfull ! We have send you Login Information to your Email. 
                <strong>Please check your Email and Login.</strong>";
        } else {
            return "<strong>Sorry !</strong> &nbsp; Invalid Operation.";
        }
    }

//-- CHECK MEMBER LOGGED-IN --//
    function check_logedin() {
        if (!$this->session->userdata('mem_email')) {
            redirect('member/login');
        }
    }

//-- CHECK MEMBER LOGIN --//
    function check_member($post) {
        if (!$post) {
            return "<strong>Sorry !</strong> &nbsp; Invalid Operation.";
        } else {
            $user = trim($post['email']);
            $pwd = trim($post['password']);
            $sql = "Select * from Member_master where email='$user' and password='$pwd' and status=51";
            $query = $this->db->query($sql);
            $user = $query->row();

            if ($query->num_rows() == 1) {
                $this->session->set_userdata('mem_id', $user->id);
                $this->session->set_userdata('mem_email', $user->email);
                $this->session->set_userdata('mem_password', $user->password);
                $this->session->set_userdata('mem_type', $user->type);
                $this->session->set_userdata('mem_fname', $user->first_name);
                $this->session->set_userdata('mem_lname', $user->last_name);
                $this->session->set_userdata('mem_last_login', $user->last_login);
                $this->session->set_userdata('mem_created_on', $user->created_on);
                //-- Update last login of successfull Login
                $sql = "update member_master set last_login = NOW() where id=$user->id";
                $query = $this->db->query($sql);
                return null;
            } else {
                return "<strong>Sorry !</strong> &nbsp; Invalid Login !";
            }
        }
    }

    function change_status($id, $status) {
        if($status==1){
          $sql="update member_master set status=51 where id=$id" ;
          }
          if($status==51){
          $sql="update member_master set status=1 where id=$id" ;
          } 
        $this->db->query($sql);
    }

//-- END OF MODEL MEMBER --//    
}

?>

